<?php 
session_start();

header("Content-type: application/rtf");
header("Content-Disposition: attachment; filename=text.rtf");
include("class_rtf.php");
include("application/base_class.php");

function getFormatEdition($edition)
{	
	$result = str_replace('st', '<sup>st</sup>', $edition);
	$result = str_replace('nd', '<sup>nd</sup>', $result);
	$result = str_replace('rd', '<sup>rd</sup>', $result);
	$result = str_replace('th', '<sup>th</sup>', $result);			
	return $result;
}

function getLastName($name)
{
	$lastName = "";
	$temp = str_replace('`', ' ', $name);
	$splitName = explode(' ', $temp);						
	$countSplitName = count($splitName);
	if ($countSplitName > 0)
	{
		$lastName = $splitName[$countSplitName - 1];
		$length = strlen($lastName);
		if ($length > 0)
		{
			$lastName = strtoupper($lastName[0]) . strtolower(substr($lastName, 1));
		}
	}		
	
	return $lastName;			
}

function getInitialFirstName($name)
{
	$initial = "";
	$temp = str_replace('`', ' ', $name);
	$splitName = explode(' ', $temp);			
	$countSplitName = count($splitName);
	if ($countSplitName > 0)
	{
		$initial = strtoupper($splitName[0]);
		$initial = $initial[0];
	}
	return $initial;
}

function doubleQuoteJS($text)
{
$result = str_replace('"', '\\"', $text);
return $result;
}

$base_obj = new BaseClass();
$list = mysql_query("select * from composedocument where username='".$_SESSION['user_id']."' and types!='compose' and title_reference='".$_SESSION['tr_dload']."' order by lastname ASC");
$citationList = "";
$citationToInsert = "";
$citationOptions = "";
$index = 1;
$references = "";

while($work = mysql_fetch_array($list)){
	$all_name = "";
	$all_name = $work['firstname'] . "`" . $work['lastname'];
	if ($work['author'] != "")
		$all_name .= "," . $work['author'];
	$exp = explode(",",$all_name);			
	$count = count($exp);
	$result = "";			
	$year = doubleQuoteJS($work['pyear']);
	if ($work['types'] == 'book')
	{				
		$lastNameJS = "";	
		$citationOptions = "";
		
		if ($count == 1)
		{				
			$lastName = getLastName($exp[0]);
			$result = $lastName . ", " . getInitialFirstName($exp[0]) . ".";
			$lastNameJS = doubleQuoteJS($lastName);										
			
			$citationOptions = $lastNameJS . '(' . $year . ')';
			$citationList .= '"citation_' . $index++ . '":"' . $citationOptions . '",';
			$citationToInsert .= str_replace(",", "_S_S_", $citationOptions) . ",";
			$citationOptions = "(" . $lastNameJS . ',' . $year . ')';
			$citationList .= '"citation_' . $index++ . '":"' . $citationOptions . '",';
			$citationToInsert .= str_replace(",", "_S_S_", $citationOptions) . ',';
		}
		else
		{
			$tempCitation = "";
			for($i = 0; $i < $count; $i++)
			{
				$lastName = getLastName($exp[$i]);					
				$tempLastNameJS = doubleQuoteJS($lastName);
				if( $i == $count - 1)
				{
					if ($count < 7)
					{
						$result .= "&" . $lastName . ", " . getInitialFirstName($exp[$i]);
					}
					else
					{
						$result .= "..." . $lastName . ", " . getInitialFirstName($exp[$i]);
					}
					
					if ($count < 3)
					{							
						$tempCitation =  substr($tempCitation, 0, strlen($tempCitation) - 2);
						
						$citationOptions = $tempCitation . ' and ' . $tempLastNameJS . '(' . $year . ')';
						$citationList .= '"citation_' . $index++ . '":"' . $citationOptions . '",';
						$citationToInsert .= str_replace(",", "_S_S_", $citationOptions) . ",";
						
						$citationOptions = "(" . $tempCitation . ' & ' . $tempLastNameJS . ',' . $year . ')';	
						$citationList .= '"citation_' . $index++ . '":"' . $citationOptions . '",';
						$citationToInsert .= str_replace(",", "_S_S_", $citationOptions) . ",";
					}
				}
				else
				{
					$result .= $lastName . ", " . getInitialFirstName($exp[$i]) . "., ";
					if ($count < 3)
					{
						$tempCitation .= $tempLastNameJS . ", ";
					}
				}						
			}
		}			
		
		if ($count >= 3)
		{				
			$lastName = getLastName($exp[0]);						
			$citationOptions = $lastName . ' et al (' . $year . ')';
			$citationList .= '"citation_' . $index++ . '":"' . $citationOptions . '",';
			$citationToInsert .= str_replace(",", "_S_S_", $citationOptions) . ",";
			
			$citationOptions = '(' . $lastName . ' et al, ' . $year . ')';
			$citationList .= '"citation_' . $index++ . '":"' . $citationOptions . '",';
			$citationToInsert .= str_replace(",", "_S_S_", $citationOptions) . ",";
		}
		if ($work['edition'] != "")
		{
			$result .= "(" . $year . "). <i>" . $work['booktitle'] . "</i>(" . 
				getFormatEdition($work['edition']) . "). " . $work['pcity'] . ": " . $work['publisher'] . ".";
		}
		else
		{
			$result .= "(" . $year . "). <i>" . $work['booktitle'] . "</i>. " . $work['pcity'] . ": " . $work['publisher'] . ".";
		}
	}
	else if ($work['types'] == 'journal')//journal
	{
		$lastNameJS = "";
		if ($count == 1)
		{	
			$lastName = getLastName($exp[0]);
			$result = $lastName . ", " . getInitialFirstName($exp[0]) . ".";
			$lastNameJS = doubleQuoteJS($lastName);										
			
			$citationOptions = $lastNameJS . '(' . $year . ',p.)';
			$citationList .= '"citation_' . $index++ . '":"' . $citationOptions . '",';
			$citationToInsert .= str_replace(",", "_S_S_", $citationOptions) . ",";
			$citationOptions = "(" . $lastNameJS . ',' . $year . ',p.)';
			$citationList .= '"citation_' . $index++ . '":"' . $citationOptions . '",';
			$citationToInsert .= str_replace(",", "_S_S_", $citationOptions) . ",";
		}
		else
		{
			$tempCitation = "";
			for($i = 0; $i < $count; $i++)
			{
				$lastName = getLastName($exp[$i]);					
				$tempLastNameJS = doubleQuoteJS($lastName);
				if( $i == $count - 1)
				{
					if ($count < 7)
					{
						$result .= "&" . $lastName . ", " . getInitialFirstName($exp[$i]);
					}
					else
					{
						$result .= "..." . $lastName . ", " . getInitialFirstName($exp[$i]);
					}
					
					if ($count < 3)
					{							
						$tempCitation =  substr($tempCitation, 0, strlen($tempCitation) - 2);
						
						$citationOptions = $tempCitation . ' and ' . $tempLastNameJS . '(' . $year . ')';
						$citationList .= '"citation_' . $index++ . '":"' . $citationOptions . '",';
						$citationToInsert .= str_replace(",", "_S_S_", $citationOptions) . ",";
						
						$citationOptions = "(" . $tempCitation . ' & ' . $tempLastNameJS . ',' . $year . ')';	
						$citationList .= '"citation_' . $index++ . '":"' . $citationOptions . '",';
						$citationToInsert .= str_replace(",", "_S_S_", $citationOptions) . ",";
					}
				}
				else
				{
					$result .= $lastName . ", " . getInitialFirstName($exp[$i]) . "., ";
					if ($count < 3)
					{
						$tempCitation .= $tempLastNameJS . ", ";
					}
				}						
			}
		}
		
		if ($count >= 3)
		{				
			$lastName = getLastName($exp[0]);						
			$citationOptions = $lastName . ' et al (' . $year . ')';
			$citationList .= '"citation_' . $index++ . '":"' . $citationOptions . '",';
			$citationToInsert .= str_replace(",", "_S_S_", $citationOptions) . ",";
			
			$citationOptions = '(' . $lastName . ' et al, ' . $year . ')';
			$citationList .= '"citation_' . $index++ . '":"' . $citationOptions . '",';
			$citationToInsert .= str_replace(",", "_S_S_", $citationOptions) . ",";
		}
		
		if ($count < 6)
		{	
			$result .= "(" . $year . "). " . $work['booktitle'] . ". <i>" . $work['journaltitle'] . "," . $work['issuenumber'] . "</i>(" . 
				$work['edition'] . "), " . $work['pagenumber'] . ".";
		}
		else
		{
			$result .= "(" . $year . "). " . $work['booktitle'] . ". <i>" . $work['journaltitle'] . "," . $work['issuenumber'] . "</i>, " . 
			$work['pagenumber'] . ".";
		}
		
		if ($work['digitalobject'] != "")
		{
			$result .= " doi:" . $work['digitalobject'];
		}
	}
	else if ($work['types'] == 'web')//website
	{
		$year = doubleQuoteJS($work['website_year']);
		$month = doubleQuoteJS($work['website_month']);
		$day = doubleQuoteJS($work['website_day']);
		
		$year_accessed = doubleQuoteJS($work['website_year_accessed']);
		$month_accessed = doubleQuoteJS($work['website_month_accessed']);
		$day_accessed = doubleQuoteJS($work['website_day_accessed']);
		
		$lastNameJS = "";
		if ($count == 1)
		{	
			$lastName = getLastName($exp[0]);
			$result = $lastName . ", " . getInitialFirstName($exp[0]) . ".";
			$lastNameJS = doubleQuoteJS($lastName);										
			
			$citationOptions = $lastNameJS . ', ' . $work['name_of_webpage'] .'(' . $year . ')';
			$citationList .= '"citation_' . $index++ . '":"' . $citationOptions . '",';
			$citationToInsert .= str_replace(",", "_S_S_", $citationOptions) . ",";
			$citationOptions = "(" . $lastNameJS . ', ' . $work['name_of_webpage'] . ',' . $year . ')';
			$citationList .= '"citation_' . $index++ . '":"' . $citationOptions . '",';
			$citationToInsert .= str_replace(",", "_S_S_", $citationOptions) . ",";
		}
		else
		{
			$tempCitation = "";
			for($i = 0; $i < $count; $i++)
			{
				$lastName = getLastName($exp[$i]);					
				$tempLastNameJS = doubleQuoteJS($lastName);
				if( $i == $count - 1)
				{
					if ($count < 7)
					{
						$result .= "&" . $lastName . ", " . getInitialFirstName($exp[$i]);
					}
					else
					{
						$result .= "..." . $lastName . ", " . getInitialFirstName($exp[$i]);
					}
					
					if ($count < 3)
					{							
						$tempCitation =  substr($tempCitation, 0, strlen($tempCitation) - 2);
						
						$citationOptions = $tempCitation . ' and ' . $tempLastNameJS . '(' . $year . ')';
						$citationList .= '"citation_' . $index++ . '":"' . $citationOptions . '",';
						$citationToInsert .= str_replace(",", "_S_S_", $citationOptions) . ",";
						
						$citationOptions = "(" . $tempCitation . ' & ' . $tempLastNameJS . ',' . $year . ')';	
						$citationList .= '"citation_' . $index++ . '":"' . $citationOptions . '",';
						$citationToInsert .= str_replace(",", "_S_S_", $citationOptions) . ",";
					}
				}
				else
				{
					$result .= $lastName . ", " . getInitialFirstName($exp[$i]) . "., ";
					if ($count < 3)
					{
						$tempCitation .= $tempLastNameJS . ", ";
					}
				}						
			}
		}
		
		if ($count >= 3)
		{				
			$lastName = getLastName($exp[0]);						
			$citationOptions = $lastName . ' et al (' . $year . ')';
			$citationList .= '"citation_' . $index++ . '":"' . $citationOptions . '",';
			$citationToInsert .= str_replace(",", "_S_S_", $citationOptions) . ",";
			
			$citationOptions = '(' . $lastName . ' et al, ' . $year . ')';
			$citationList .= '"citation_' . $index++ . '":"' . $citationOptions . '",';
			$citationToInsert .= str_replace(",", "_S_S_", $citationOptions) . ",";
		}
		
		if ($count < 6)
		{	
			$result .= "(" . $year . "," . $month . " " . $day . ")<i>. de. Retrieved</i>. " . 
				$day_accessed . " " . $month_accessed . ", " . $year_accessed ."), from " . $work['name_of_webpage'] . ": ". $work['website_url'] . ".";
		}
		else
		{
			$result .= "(" . $year . "," . $month . " " . $day . ")<i>. de. Retrieved</i>. " . 
				$day_accessed . " " . $month_accessed . ", " . $year_accessed ."), from " . $work['name_of_webpage'] . ": ". $work['website_url'] . ".";
		}
				
	}
	
	$references .= $result . "<br>";
}

$body = mysql_fetch_array(mysql_query("select * from composedocument where id=(select MAX(id) from composedocument where username='".$_SESSION['user_id']."' and types='compose')"));
 
$comments = $body['comments'];
//$assainment = "<div style=\"text-align: center;\">".$comments."</div><div style=\"text-align: center;\"><b>REFERENCES</b></div><div>" . $references . "</div>";
$assainment = "<div style=\"text-align: center;\"><b>REFERENCES</b></div><div>" . $references . "</div>";


$rtf = new rtf("rtf_config.php");
$rtf->setPaperSize(5);
$rtf->setPaperOrientation(1);
$rtf->setDefaultFontFace(0);
$rtf->setDefaultFontSize(20);
$rtf->setAuthor("Obby");
$rtf->setOperator("solor.motor@gmail.com");
$rtf->setTitle("RTF Document");
$rtf->addColour("#000000");
$rtf->addText($assainment);
echo $rtf->getDocument();
?>